package logic

import (
	"2210a/commod/models/mysql"
	"2210a/commod/models/redisinit"
	"context"
	"encoding/json"

	"2210a/commod/commodrpc/commodrpc"
	"2210a/commod/commodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLikeLogic {
	return &ShopListLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopListLikeLogic) ShopListLike(in *commodrpc.ShopListLikeRequest) (*commodrpc.ShopListLikeResponse, error) {
	// todo: add your logic here and delete this line
	scores := redisinit.ClientRedis.ZRevRangeWithScores("ShopLike", 0, 9)
	var products mysql.Products
	var slice []*commodrpc.ShopListLike
	for _, v := range scores.Val() {
		json.Unmarshal([]byte(v.Member.(string)), &products)
		like := commodrpc.ShopListLike{
			Id:             int64(products.ID),
			UserId:         products.UserId,
			Avatar:         products.Avatar,
			ShopName:       products.ShopName,
			Price:          products.Price,
			Gender:         products.Gender,
			ShopNavigation: products.ShopNavigation,
			Address:        products.Address,
			Quantity:       products.Quantity,
			Introduce:      products.Introduce,
			Status:         products.Status,
			Likes:          int64(v.Score),
		}
		slice = append(slice, &like)
	}
	return &commodrpc.ShopListLikeResponse{List: slice}, nil
}
